package com.ruoyi.framework.config.properties;

import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.ruoyi.common.utils.SecurityUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.sql.Connection;
import java.sql.SQLException;

@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class CustomTenantLineInnerInterceptor extends TenantLineInnerInterceptor {

    public CustomTenantLineInnerInterceptor() {
        super();
    }

    public CustomTenantLineInnerInterceptor(TenantLineHandler tenantLineHandler) {
        super(tenantLineHandler);
    }
    //private static List<String> IGNORE_STATEMENT_NAMES = Lists.newArrayList("com.fs.zhnm.edge.data.center.accountauth.dao.AccountInfoDao.getLoginAccountRole");

    @Override
    public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        // 如果无法获取用户，不走拦截器
        try{
            if (SecurityUtils.getLoginUser() == null) {return;}
        }catch (Exception e){  
            return;
        }

       // if (IGNORE_STATEMENT_NAMES.contains(ms.getId())) return;
        super.beforeQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql);
    }

    @Override
    public void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout){
        try{
            if (SecurityUtils.getLoginUser() == null) {return;}
        }catch (Exception e){
            return;
        }

        // if (IGNORE_STATEMENT_NAMES.contains(ms.getId())) return;
        super.beforePrepare(sh,connection,transactionTimeout);
    }
}
